package Groups;

import Population.Inhabitant;
import World.*;

public class Wanderer extends Group {

	private static String gType = "Wanderer";
	public int reqSize = 0;
	public int reqLength = 0;
	
	int NOMAD_TIME_TRIGGER = 150;
	int NOMAD_NUMBER_TRIGGER = 3;
	
	public Wanderer(Inhabitant[] founders) 
	{
		super(founders, gType);
		
		
	}

	@Override
	public void Update() 
	{
		// Check for new nomds
		for(int i=0; i<members.length;i++)
		{
			if(members[i].getFriends().getLength()>0)
			{ // check friends for a group to join
				for(int j=0; j<members[i].getFriends().getLength(); j++)
				{
					if(members[i].getFriends().getFriends()[j].getGroupName().contains("Nomad"))
					{
						AddToGroupOf(members[i],members[i].getFriends().getFriends()[j]);
						j = members[i].getFriends().getLength();
					}
				}
			}
			if(members[i].getFriends().getLength()>2)
			{ // check friends for a group to form
				int count = 0;
				Inhabitant[] founders = new Inhabitant[1];
				founders[0] = members[i];
				for(int j=0;j<members[i].getFriends().getLength();j++)
				{
					if(members[i].getFriends().GetFriendRepeat()[j] > NOMAD_TIME_TRIGGER)
					{
						founders = new Inhabitant[founders.length];
						founders[founders.length-1] = members[i].getFriends().getFriends()[j];
						count++;
					}
				}
				if(count > NOMAD_NUMBER_TRIGGER)
				{// Form group
					World.pop.AddGroup_Nomad(founders);
				}
			}
		}
		// Individual movements
		for(int i=0;i<members.length; i++)
		{
			members[i].AI();
		}

	}

	@Override
	public String GenerateGroupName() 
	{
		return gType;
	}

	@Override
	public int GetReqTriggerSize() {
		return reqSize;
	}

	@Override
	public int GetReqTriggerTime() {
		return reqLength;
	}

	@Override
	public void Move() {
		// TODO Auto-generated method stub
		
	}
	
	public void AddToGroupOf(Inhabitant newMember, Inhabitant groupMember)
	{
		for(int i=0; i<World.pop.groups.length; i++)
		{
			if(World.pop.groups[i].contains(groupMember))
			{
				World.pop.groups[i].AddMember(newMember);
			}
		}
	}

}
